<?php
namespace app\api\model;

use app\model\Base;

class Union extends Base
{
    public function getDiscountFormatAttr($value, $data)
    {
        return '商铺打' . ((100 - $data['discount']) / 10) . '折';
    }

    public function getFaciaFormatAttr($value, $data)
    {
        $str = '';
//        if(!empty($data['facia'])){
//            $str = $this::getNewUrl() . $data['facia'];
//        }
        if(!empty($data['store_entrance_pic'])){
            $str = $this::getNewUrl() . $data['store_entrance_pic'];
        }
        return $str;
    }

    public function getStatusFormatAttr($value, $data)
    {
        $str = '已通过';
        if($data['status'] == 0){
            $str = '审核中';
        }
        return $str;
    }

    public static function induce($unionId, $orderId, $amount, $wallet_type = 23, $wallet_des = 23, $alias = 'points', $des = '')
    {
        /**
         * TODO
         * $type 判断是否需要手续费
         * 手续费比例后台配置，直接获取后台手续费比例
         * 加手续费字段，扣除手续费逻辑
         */
        $item = self::find($unionId);
        $add = [];
        $add['union_id'] = $unionId;
        $add['order_id'] = $orderId;
        $add['amount'] = $amount;
        $add['type'] = Base::$walletTypeArray[$wallet_type];
        $add['content'] = $des == '' ? Base::$walletLogDesArray[$wallet_des] : $des;
        $add['before'] = $item[$alias];

        $update = [];
        $update[$alias] = $add['before'] + $amount;

        $item->save($update);
        switch ($alias)
        {
            case 'points':{
                UnionPointsLog::create($add);
            }
                break;
        }
        return true;
    }
}